package com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.pojo.User2;
import com.example.demo.service.UserServiceImpl;
import com.example.demo.service.UserServiceImpl2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    private int number = 1;
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserServiceImpl2 userService2;

    /*
         http://localhost:8006/userList  端口需要查看 application.yml 里的配置

        [
             {
                "id": 1,
                "username": "dragon",
                "email": null,
                "age": 20,
                "sex": "男"
               },
             {
                "id": 2,
                "username": "admin",
                "email": null,
                "age": 20,
                "sex": "男"
              }
        ]
     */
    // 查询全部

    @RequestMapping("/userList")//链接路径 /user
    public List<User> userList() {
        return userService.queryUserList();
    }

    @RequestMapping("/addUser")//链接路径  http://localhost:8006/addUser
    public String addUser(){
        User user = new User();
        user.setUsername("mbatis-"+(number++));
        user.setEmail("mybatis@test.com");
        user.setPassword("123456");
        user.setCreate_time(new Date(System.currentTimeMillis()));
        user.setAge(21);
        user.setSex("男");
        user.setId(66666+number);

        boolean insertResult = userService.addUser(user);
        String ir = "插入数据是成功 insertResult = " + insertResult + " , " + user.toString();
        System.out.println(ir);
        //插入数据是成功 insertResult = true , User(id=66666, username=mbatis, email=mybatis@test.com, password=123456, create_time=Tue Nov 29 12:18:37 CST 2022, sex=男, age=16)

        return ir;
    }

    @RequestMapping("/userList2")//链接路径  http://localhost:8006/userList2
    public List<User2> userList2() {
        return userService2.queryUserList2();
    }

    @RequestMapping("/addUser2")//链接路径  http://localhost:8006/addUser2
    public String addUser2(){
        User2 user = new User2();
        user.setUsername("mbatis-"+(number++));
        user.setEmail("mybatis@test.com");
        user.setPassword("123zzz");
        user.setCreate_time(new Date(System.currentTimeMillis()));
        user.setAge(30+number);
        if (number % 2 == 0) {
            user.setSex("女");
        }else {
            user.setSex("男");
        }

//        user.setId(66666+number+1);

        boolean insertResult = userService2.addUser2(user);
        String ir = "插入数据是成功 insertResult = " + insertResult + " , " + user.toString();
        System.out.println(ir);
        //插入数据是成功 insertResult = true , User(id=66666, username=mbatis, email=mybatis@test.com, password=123456, create_time=Tue Nov 29 12:18:37 CST 2022, sex=男, age=16)

        return ir;
    }
}
